<template>
	<view class="content">
		<form @submit="formSubmit">
			<view class="logo">
				<image :src="userInfo.img" @tap="image"></image>
				<!-- <tui-icon name="add" color='#5677fc' @tap="image"></tui-icon> -->
			</view>
			<view class="uni-form-item uni-column">
				<input type="tel" class="uni-input" name="mobile" v-model="userInfo.userId" placeholder="请输入手机号" />
			</view>
			<view class="uni-form-item uni-column">
				<input type="text" class="uni-input" name="name" v-model="userInfo.user" placeholder="请输入用户名" />
			</view>
			<view class="uni-form-item uni-column">
				邀请码<input type="text" class="uni-input" name="invitationcode" v-model="userInfo.invitationcode" placeholder="将获得3个积分" />
			</view>
			<view class="uni-form-item uni-column">
				<input type="password" class="uni-input" name="passWord" v-model="userInfo.passWord" placeholder="请输入密码" />
			</view>
			<view class="uni-form-item uni-column column-with-btn">
				<input type="number" class="uni-input" name="ycode" v-model="userInfo.ycode" placeholder="请输入验证码" />
				<button :class="{active : !disableCodeBtn}" :disabled="disableCodeBtn" @tap="sendCode">{{codeBtn.text}}</button>
			</view>
			<view class="uni-form-item uni-column">
				<textarea type="tel" class="uni-input" name="introduce" v-model="userInfo.introduce" placeholder="脱恐宣言（或个人经历描述）" />
				</view>
			<radio-group @change="radioChange">
				<!-- <label class="radio-change" v-for="(item, index) in items" :key="item.value">
					<radio :value="item.value" :checked="index+1 === current" />{{item.name}}
				</label> -->
				<label class="radio-change" >
					<radio value="1" checked="true" />普通用户</label>
				</label> 
				<label class="radio-change" @tap="radioTitle">
					<radio value="2" disabled="true" />专家</label>
				</label>
			</radio-group>
			
			
			<yonghu-tags ref="yonghuTagRef" ></yonghu-tags>
			<button class="tui-btn" formType="submit">注册</button>
			<view class="links">已有账号？<view class="link-highlight" @tap="gotoLogin">点此登陆</view>
			</view>
		</form>
			</view>
</template>

<script>
	const form = require("../../components/utils/formValidation.js")
	import yonghuTags from '@/components/xinliziliao/yonghuTags'
	import uploadImage from '@/js_sdk/yushijie-ossutil/ossutil/uploadFile.js';
	import tuiIcon from "@/components/icon/icon"
	export default {
		components: {
			yonghuTags,
			tuiIcon
		},
		data() {
			return {
				tags: [{
					shape: '123',
					name: '123fdsaf',
				}],
				captchaImg: '',
				seconds: 60,
				codeBtn: {
					text: '获取验证码',
					waitingCode: false,
					count: this.seconds
				},
				items: [{
						value: '1',
						name: '普通用户'
					},
					{
						value: '2',
						name: '专家'
					}
				],
				current: 1,
				userInfo: {
					user: '',
					userId: '',
					passWord: '',
					introduce : '',
					userSig: '1',
					img: '../../static/add.png',
					invitationcode : '',
					ycode  : ''
				},
				codes: []
			}
		},
		onLoad(e) {
			// #ifndef H5
			uni.showShareMenu({
				showShareItems : ['qq', 'qzone', 'wechatFriends', 'wechatMoment']	
			});
			// #endif
			
			let user = this.$xinli.user.getUserInfo();
			// 在用户自己手机上的.
			if (user) {
				this.userInfo = user;
			}
			// 在
			if(e.invitationcode){
				this.userInfo.invitationcode = e.invitationcode;
			}
			// 
			console.log(this.userInfo);
		},
		methods: {
			image: function() {
				//上传图片
				const that = this;
					uni.chooseImage({
					    count: 1, // 默认最多一次选择9张图
					    sourceType: ['camera','album'], // 可以指定来源是相册还是相机，默认二者都有
					    success: res=> {
					        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
					        var tempFilePaths = res.tempFilePaths;
							
							// 修改头像地址. 
							let url =  uploadImage(res.tempFilePaths[0], `touxiang/${that.userInfo.userId}.png`
							, url0 => {
								url += '?t='+new Date().getTime();
								that.userInfo.img = url;
							})
							
					        //支持多图上传
					        // for (var i = 0; i < res.tempFilePaths.length; i++) {
					        // }
					    }
					})
			},
			formSubmit: function(e) {
				//表单规则
				let rules = [{
					name: "mobile",
					rule: ["required", "isMobile"],
					msg: ["请输入手机号", "请输入正确的手机号"]
				}, {
					name: "name",
					rule: ["required", "isChinese", "minLength:2", "maxLength:6"], //可使用区间，此处主要测试功能
					msg: ["请输入姓名", "姓名必须全部为中文", "姓名必须2个或以上字符", "姓名不能超过6个字符"]
				}, {
					name: "passWord",
					rule: ["required", "minLength:2", "maxLength:16"], //可使用区间，此处主要测试功能
					msg: ["请输入密码", "密码必须2个或以上字符", "密码不能超过16个字符"]
				},{
					name: "ycode",
					rule: ["required"], //可使用区间，此处主要测试功能
					msg: ["请输入验证码"]
				}]; // 
				//进行表单检查
				let formData = e.detail.value;
				let checkRes = form.validation(formData, rules);
				let imgUrl = this.userInfo.img;
				
				console.log(formData,checkRes);
				
				if (imgUrl === '../../static/add.png') {
					uni.showToast({
						title: '请上传头像',
						icon: "none"
					});
				}else if(!checkRes){
					const data = this.userInfo;
					data.type = this.current;
					data.yonghuBiaoQian = this.codes
					data.yonghuBiaoQian = data.yonghuBiaoQian.concat(this.$refs.yonghuTagRef.getClickTags());
					if( data.yonghuBiaoQian.length > 0 ){
						this.$api.userRegister(data).then(res => {
							console.log(res);
							uni.setStorageSync('userInfo', res.data);
							uni.reLaunch({
								url: '/pages/index/index'
							})
						})
						console.log(data, this.codes)
					}else{
						uni.showToast({
							title: '请选择标签',
							icon: "none"
						});
					}
				}else {
					uni.showToast({
						title: checkRes,
						icon: "none"
					});
				}
			},
			sendCode: function() {
				this.codeBtn.waitingCode = true;
				this.codeBtn.count = this.seconds;
				this.codeBtn.text = this.codeBtn.count + 's';
				let countdown = setInterval(() => {
					this.codeBtn.count--;
					this.codeBtn.text = this.codeBtn.count + 's';
					if (this.codeBtn.count < 0) {
						clearInterval(countdown);
						this.codeBtn.text = '重新发送';
						this.codeBtn.waitingCode = false;
					}
				}, 1000);
				this.$apiW.sendCode({
					phone : this.userInfo.userId
				});
			},
			gotoLogin: function() {
				uni.navigateTo({
					url: 'login'
				})
			},
			radioTitle: function() {
				uni.showToast({
					title: '暂时不对外开放专家注册~',
					icon: "none"
				})
			},
			radioChange: function(evt) {
				let current = 0 ;
				for (let i = 0; i < this.items.length; i++) {
					if (this.items[i].value === evt.target.value) {
						current = this.items[i].value;
						//break;
					}
				}
				console.log(current);
				if( current == 2){
					
					evt.target.value = 1; 
					this.current = 1;
				}
			},
			register: function() {
				const data = this.userInfo;
				data.type = this.current;
				data.yonghuBiaoQian = this.codes

				//表单规则  
				let rules = [{
					name: "name",
					rule: ["required", "isChinese", "minLength:2", "maxLength:6"], //可使用区间，此处主要测试功能
					msg: ["请输入姓名", "姓名必须全部为中文", "姓名必须2个或以上字符", "姓名不能超过6个字符"]
				}, {
					name: "mobile",
					rule: ["required", "isMobile"],
					msg: ["请输入手机号", "请输入正确的手机号"]
				}];
				//进行表单检查
				let formData = this.userInfo;
				let checkRes = form.validation(formData, rules);
				console.log(checkRes);
				if (!checkRes) {

				} else {
					uni.showToast({
						title: checkRes,
						icon: "none"
					});
				}

			},
			toTags: function() {
				const that = this;
				uni.setStorageSync('userInfo', that.userInfo);
				uni.navigateTo({
					url: 'tags'
				})
			}
		},
		computed: {
			disableCodeBtn: function() {
				return this.codeBtn.waitingCode ;
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '../../static/style/thorui.css';
	$color-primary: #b49950;
	.tui-icon-add{
		font-size: 100px !important;
		color: #999 !important;
	}
	.tui-btn{
		margin-top: 30upx;
		background-color: #df5451;
		color: #fff;
	}
	.title {
		padding: 30upx 0;
		font-size: 32upx;
		color: #333;
		font-weight: bold;
	}
	.content {
		padding: 60upx 100upx 100upx;
	}

	.logo {
		text-align: center;
		image {
			height: 200upx;
			width: 200upx;
			margin: 0 0 40upx;
			border-radius: 50%;
		}
	}

	.uni-form-item {
		margin-bottom: 40upx;
		padding: 0;
		border-bottom: 1px solid #e3e3e3;

		.uni-input {
			font-size: 30upx;
			padding: 7px 0;
		}
	}

	.column-with-btn {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;

		button {
			font-size: 24upx;
			margin: 0;
			width: 180upx;
			text-align: center;

			&:after {
				border: none
			}

			&.active {
				background-color: $color-primary;
				color: $uni-text-color-inverse;
			}
		}
	}

	.img-captcha {
		width: 150upx;
		height: 60upx;
	}

	button[type="primary"] {
		background-color: $color-primary;
		border-radius: 0;
		font-size: 34upx;
		margin-top: 60upx;
	}

	.links {
		text-align: center;
		margin-top: 40upx;
		font-size: 26upx;
		color: #999;

		view {
			display: inline-block;
			vertical-align: top;
			margin: 0 10upx;
		}

		.link-highlight {
			color: $color-primary
		}
	}

	.radio-change {
		margin-right: 50upx;
	}
</style>
